package cn.iocoder.yudao.module.eval.service.announcement;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.eval.controller.admin.announcement.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.announcement.EvalAnnouncementDO;
import cn.iocoder.yudao.module.eval.dal.mysql.announcement.EvalAnnouncementMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.eval.enums.ErrorCodeConstants.*;

/**
 * 公告发布 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class EvalAnnouncementServiceImpl implements EvalAnnouncementService {

    @Resource
    private EvalAnnouncementMapper announcementMapper;

    @Override
    public Long createAnnouncement(EvalAnnouncementSaveReqVO createReqVO) {
        // 插入
        EvalAnnouncementDO announcement = BeanUtils.toBean(createReqVO, EvalAnnouncementDO.class);
        announcement.setPublishStatus(0); // 草稿状态
        announcement.setViewCount(0);
        announcement.setDownloadCount(0);
        announcement.setObjectionCount(0);
        announcementMapper.insert(announcement);
        // 返回
        return announcement.getId();
    }

    @Override
    public void updateAnnouncement(EvalAnnouncementSaveReqVO updateReqVO) {
        // 校验存在
        validateAnnouncementExists(updateReqVO.getId());
        // 更新
        EvalAnnouncementDO updateObj = BeanUtils.toBean(updateReqVO, EvalAnnouncementDO.class);
        announcementMapper.updateById(updateObj);
    }

    @Override
    public void deleteAnnouncement(Long id) {
        // 校验存在
        validateAnnouncementExists(id);
        // 删除
        announcementMapper.deleteById(id);
    }

    private void validateAnnouncementExists(Long id) {
        if (announcementMapper.selectById(id) == null) {
            throw exception(ANNOUNCEMENT_NOT_EXISTS);
        }
    }

    @Override
    public EvalAnnouncementDO getAnnouncement(Long id) {
        return announcementMapper.selectById(id);
    }

    @Override
    public PageResult<EvalAnnouncementDO> getAnnouncementPage(EvalAnnouncementPageReqVO pageReqVO) {
        return announcementMapper.selectPage(pageReqVO);
    }

    @Override
    public List<EvalAnnouncementDO> getAnnouncementList(EvalAnnouncementPageReqVO exportReqVO) {
        return announcementMapper.selectList(exportReqVO);
    }

    @Override
    public void publishAnnouncement(Long id) {
        // 校验存在
        EvalAnnouncementDO announcement = getAnnouncement(id);
        if (announcement == null) {
            throw exception(ANNOUNCEMENT_NOT_EXISTS);
        }

        // 更新发布状态
        EvalAnnouncementDO updateObj = new EvalAnnouncementDO();
        updateObj.setId(id);
        updateObj.setPublishStatus(3); // 已发布
        updateObj.setPublishTime(LocalDateTime.now());
        announcementMapper.updateById(updateObj);
    }

    @Override
    public void withdrawAnnouncement(Long id) {
        // 校验存在
        validateAnnouncementExists(id);

        // 更新撤回状态
        EvalAnnouncementDO updateObj = new EvalAnnouncementDO();
        updateObj.setId(id);
        updateObj.setPublishStatus(4); // 已撤回
        announcementMapper.updateById(updateObj);
    }

    @Override
    public void topAnnouncement(Long id, Integer topOrder) {
        // 校验存在
        validateAnnouncementExists(id);

        // 更新置顶状态
        EvalAnnouncementDO updateObj = new EvalAnnouncementDO();
        updateObj.setId(id);
        // 注意：这里需要在DO类中添加isTop和topOrder字段
        announcementMapper.updateById(updateObj);
    }

    @Override
    public void cancelTopAnnouncement(Long id) {
        // 校验存在
        validateAnnouncementExists(id);

        // 取消置顶
        EvalAnnouncementDO updateObj = new EvalAnnouncementDO();
        updateObj.setId(id);
        // 注意：这里需要在DO类中添加isTop字段
        announcementMapper.updateById(updateObj);
    }

    @Override
    public List<EvalAnnouncementDO> getAnnouncementListByPlanId(Long planId) {
        return announcementMapper.selectListByPlanId(planId);
    }

    @Override
    public Long generatePlanAnnouncement(Long planId, @Valid EvalAnnouncementGenerateReqVO generateReqVO) {
        // TODO: 实现生成计划公告逻辑
        return 1L;
    }

    @Override
    public Long generateScoreAnnouncement(Long planId, @Valid EvalScoreAnnouncementGenerateReqVO generateReqVO) {
        // TODO: 实现生成成绩公告逻辑
        return 1L;
    }

    @Override
    public List<EvalAnnouncementRespVO> getPlanAnnouncements(Long planId, String announcementType, Integer status) {
        // TODO: 实现获取计划公告逻辑
        return List.of();
    }

    @Override
    public PageResult<EvalAnnouncementRespVO> getPublicAnnouncements(String announcementType, String keyword, @Valid EvalAnnouncementPageReqVO pageReqVO) {
        // TODO: 实现获取公开公告分页逻辑
        return new PageResult<>(List.of(), 0L);
    }

    @Override
    public EvalAnnouncementRespVO getPublicAnnouncement(Long id) {
        // TODO: 实现获取公开公告详情逻辑
        return new EvalAnnouncementRespVO();
    }

    @Override
    public void recordAnnouncementView(Long id) {
        // TODO: 实现记录公告查看逻辑
    }

    @Override
    public EvalAnnouncementStatisticsRespVO getAnnouncementStatistics(Long planId) {
        // TODO: 实现获取公告统计逻辑
        return new EvalAnnouncementStatisticsRespVO();
    }

    @Override
    public void sendAnnouncementNotification(Long id, @Valid EvalAnnouncementNotificationReqVO notificationReqVO) {
        // TODO: 实现发送公告通知逻辑
    }

}
